package com.xiaoq.sites.repository.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.sites.entity.ActShowEntity;
import com.xiaoq.sites.repository.IActShowRepository;
import com.xiaoq.tool.util.IdGeneratorUtil;
import com.xiaoq.vo.PagingVo;

@Repository
public class ActShowRepositoryImpl implements IActShowRepository {
	
	@Autowired
	private Datastore dataStore;
	
	private Logger logger = LoggerFactory.getLogger(ActShowRepositoryImpl.class);

	@Override
	public List<ActShowEntity> findActShow(String orgnizerId, PagingVo pvo, Long time) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		Long total = 0L;
		
		Query<ActShowEntity> actShowQuery = dataStore.find(ActShowEntity.class);
		if (orgnizerId != null) {
			actShowQuery = actShowQuery.field("orgnizerId").equal(orgnizerId);
		}
		// 当日活动不再根据
//		if(time!=null&&time!=0L){
//			actShowQuery = actShowQuery.filter("startTime <=", time).filter("endTime >=", time);
//		}
		
		total = actShowQuery.count();
		actShowQuery = actShowQuery.order("-createdAt").offset(pn).limit(ps);
		List<ActShowEntity> content = actShowQuery.asList();
		pvo.setTc(total);
		return content;
	}

	@Override
	public boolean addActShow(ActShowEntity actShow) {
		if(actShow!=null){
			dataStore.save(actShow);
			return true;
		}
		logger.error("传递参数不能为空");
		return false;
	}

	@Override
	public boolean delActShow(String id) {
		if(StringUtils.isNotEmpty(id)){
			Query<ActShowEntity> actShowQuery = dataStore.find(ActShowEntity.class).field(IdGeneratorUtil.IDGENERATOR).equal(id);
			dataStore.delete(actShowQuery);
			return true;
		}
		logger.warn("必须知道今日活动的Id！");
		return false;
	}

	@Override
	public boolean editActShow(ActShowEntity actShow) {
		if(actShow!=null){
			String id = actShow.getId();
			UpdateOperations<ActShowEntity> actShowUpdate = dataStore.createUpdateOperations(ActShowEntity.class);
			if(StringUtils.isNotEmpty(id)){
				Query<ActShowEntity> actShowQuery = dataStore.find(ActShowEntity.class).field(IdGeneratorUtil.IDGENERATOR).equal(id);
				if(actShow.getTitle()!=null){
					actShowUpdate.set("title", actShow.getTitle());
				}
				if(actShow.getIntroduction()!=null){
					actShowUpdate.set("introduction", actShow.getIntroduction());
				}
				if(actShow.getStartTime()!=null){
					actShowUpdate.set("startTime", actShow.getStartTime());
				}
				if(actShow.getEndTime()!=null){
					actShowUpdate.set("endTime", actShow.getEndTime());
				}
				if(actShow.getBeginTime()!=null){
					actShowUpdate.set("beginTime", actShow.getBeginTime());
				}
				if(actShow.getTermTime()!=null){
					actShowUpdate.set("termTime", actShow.getTermTime());
				}
				if(actShow.getAddress()!=null){
					actShowUpdate.set("address", actShow.getAddress());
				}
				if(actShow.getLongitude()!=0){
					actShowUpdate.set("longitude", actShow.getLongitude());
				}
				if(actShow.getLatitude()!=0){
					actShowUpdate.set("latitude", actShow.getLatitude());
				}
				if(actShow.getOrgnizerId()!=null){
					actShowUpdate.set("orgnizerId", actShow.getOrgnizerId());
				}
				if(actShow.getOrginzerType()!=null){
					actShowUpdate.set("orginzerType", actShow.getOrginzerType());
				}
				if(actShow.getTimeString()!=null){
					actShowUpdate.set("timeString", actShow.getTimeString());
				}
				if(actShow.getHoldTimes()!=null&&actShow.getHoldTimes().size()!=0){
					actShowUpdate.set("holdTimes", actShow.getHoldTimes());
				}
				actShowUpdate.set("updatedAt", System.currentTimeMillis());
				actShowUpdate.set("pubTime", System.currentTimeMillis());
				dataStore.update(actShowQuery, actShowUpdate);
				return true;
			}else{
				logger.info("必须知道今日活动的id才能进行修改！");
			}
		}
		logger.warn("传递的参数类不能为空！");
		return false;
	}

	@Override
	public ActShowEntity getActShowDetail(String id) {
		ActShowEntity actShow = null;
		if(StringUtils.isNotEmpty(id)){
			actShow = dataStore.get(ActShowEntity.class, id);
		}
		
		return actShow;
	}
	

	

}
